<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Catalog product model
 *
 * @method Mage_Catalog_Model_Resource_Product getResource()
 * @method Mage_Catalog_Model_Resource_Product _getResource()
 *
 * @category   Mage
 * @package    Mage_Catalog
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Verz_Home_Model_Catalog_Product extends Mage_Catalog_Model_Product
{
     public function getProductList($category_id=false, $page,$search=false){
        
        $number_mix_page = DEFAULT_NUMBER_CATE_MIX_PAGE;
        if(!empty($category_id)){
            $collection = Mage::getModel("catalog/category")->load($category_id)->getProductCollection();
        }
        else{
            $collection = Mage::getModel("catalog/product")->getCollection();
        }
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
        $collection->addAttributeToSelect('name')
            ->addAttributeToSelect('description')
            ->addAttributeToSelect('thumbnail')
            ->addAttributeToSelect('is_active')
            ->addAttributeToSelect('url_key');
        
        if(!empty($search)){
            $collection->addAttributeToFilter(array(
                                array(
                                        'attribute' => 'name',
                                        'like'        => "%$search%",
                                        ),
                                    array(
                                        'attribute' => 'description',
                                        'like'      => "%$search%",
                                        )));

        }
        
            
        $collection->addAttributeToFilter('status',1)
            ->addAttributeToFilter('type_id',"simple");
            
        
        $collection->joinField('stockitem',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                'is_in_stock=1',
                '{{table}}.stock_id=1',
                'join'); 
        if(!empty($page)){
            $collection->setPage($page, $number_mix_page);
        }
        
        /*        
        if ($sorted) {
            if (is_string($sorted)) {
                // $sorted is supposed to be attribute name
                $collection->addAttributeToSort($sorted);
            } else {
                $collection->addAttributeToSort('name');
            }
        }
        */
        
        return $collection;
    }
    public function getPagination($category_id = false,$page_number,$search=false){
        
        $total = $this->countProductList($category_id,$search);
        $number_mix_page = DEFAULT_NUMBER_CATE_MIX_PAGE;
        $last = ceil($total/$number_mix_page); 
        
        //this makes sure the page number isn't below one, or more than our maximum pages 
        if ($page_number < 1) 
        { 
            $page_number = 1; 
        } 
        elseif ($page_number > $last) 
        { 
            $page_number = $last; 
        } 
        //This sets the range to display in our query 
        return array("total"=>$total,
                     "page_number"=>intval($page_number),
                     "last"=>  intval($last),
                     "from"=>($page_number - 1) * $number_mix_page,
                     "to"=>$page_number * $number_mix_page);
        
    }    
    public function countProductList($category_id=false,$search=false){
        //$collection = Mage::getModel('home_catalog/product')->getCollection();
        
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        if(!empty($category_id)){
            $collection = Mage::getModel("catalog/category")->load($category_id)->getProductCollection();
        }
        else{
            $collection = Mage::getModel("catalog/product")->getCollection();
        }
        
        
        $collection->addAttributeToSelect("count(*)")
            
            ->addAttributeToFilter('type_id',"simple")
            ->addAttributeToFilter('status',1);
            
            if(!empty($search)){
                $collection->addAttributeToFilter(array(
                                    array(
                                            'attribute' => 'name',
                                            'like'        => "%$search%",
                                            ),
                                        array(
                                            'attribute' => 'description',
                                            'like'      => "%$search%",
                                            )));

            }
            
            $collection->joinField('stockitem',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                'is_in_stock=1',
                '{{table}}.stock_id=1',
                'join'); 
            
        $total = $collection->count();
        
        return $total;
    }
}
